package com.et.machine.manager.vm.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.et.machine.R
import com.et.machine.convert.machineInfo
import com.et.machine.databinding.FragmentHomeBinding
import com.et.util.shell


class HomeFragment : Fragment() {

    companion object {
        fun newInstance() = HomeFragment()
    }

    private lateinit var viewModel: HomeViewModel
    private lateinit var binding: FragmentHomeBinding
    private lateinit var builder: AlertDialog.Builder

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = FragmentHomeBinding.inflate(inflater, container, false).apply {
        binding = this
    }.root

    @SuppressLint("SetTextI18n")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(HomeViewModel::class.java)

        initBar()

        binding.reboot.setOnClickListener {
            showDialog()
        }

        binding.btnNavVersion.setOnClickListener {
            Navigation.findNavController(it)
                .navigate(HomeFragmentDirections.actionHomeFragmentToVersionFragment())
        }

        binding.btnNavGoods.setOnClickListener {
            Navigation.findNavController(it)
                .navigate(HomeFragmentDirections.actionHomeFragmentToGoodsFragment())
        }
        binding.btnNavSetting.setOnClickListener {
            Navigation.findNavController(it)
                .navigate(HomeFragmentDirections.actionHomeFragmentToSettingFragment())
        }
        binding.btnNavTest.setOnClickListener {
            Navigation.findNavController(it)
                .navigate(HomeFragmentDirections.actionHomeFragmentToChannelTestFragment2())
        }

        binding.btnNavAdd.setOnClickListener {
            Navigation.findNavController(it)
                .navigate(HomeFragmentDirections.actionHomeFragmentToAddGoodsFragment())
        }

        binding.btnNavInternet.setOnClickListener {
            Navigation.findNavController(it)
                .navigate(HomeFragmentDirections.actionHomeFragmentToInternetFragment())
        }

//
//        binding.testProviderQuery.setOnClickListener {
//            GoodsProvider.getAll(requireContext())
//        }
//        binding.testProviderInsert.setOnClickListener {
//            GoodsProvider.insertGoods(
//                requireContext(),
//                ChannelEntity.create(11, abs(System.currentTimeMillis().toInt() % 24 + 1)).apply {
//                    priceCent = 666
//                    name = "my"
//                    barcode = "111"
//                    status = ENABLED
//                })
//            GoodsProvider.insertGoods(
//                requireContext(),
//                ChannelEntity.create(9, abs(System.currentTimeMillis().toInt() % 48 + 1)).apply {
//                    priceCent = 998
//                    name = "smq"
//                    barcode = "9001"
//                    status = ENABLED
//                })
//            GoodsProvider.insertGoods(
//                requireContext(),
//                ChannelEntity.create(8, abs(System.currentTimeMillis().toInt() % 12 + 1)).apply {
//                    priceCent = 999
//                    name = "touch"
//                    barcode = "8001"
//                    status = ENABLED
//                })
//            GoodsProvider.insertGoods(
//                requireContext(),
//                ChannelEntity.create(1, abs(System.currentTimeMillis().toInt() % 40 + 1)).apply {
//                    priceCent = 123
//                    name = "easy"
//                    barcode = "4001"
//                    status = ENABLED
//                })
//        }
//
//        binding.testProviderDelete.setOnClickListener {
//            GoodsProvider.deleteGoods(
//                requireContext(),
//                ChannelEntity.create(11, 0).uid
//            )
//            GoodsProvider.deleteGoods(
//                requireContext(),
//                ChannelEntity.create(9, 0).uid
//            )
//            GoodsProvider.deleteGoods(
//                requireContext(),
//                ChannelEntity.create(8, 0).uid
//            )
//            GoodsProvider.deleteGoods(
//                requireContext(),
//                ChannelEntity.create(1, 0).uid
//            )
//        }
//
//        binding.testProviderUpdate.setOnClickListener {
//            GoodsProvider.updateGoods(
//                requireContext(),
//                ChannelEntity.create(11, abs(System.currentTimeMillis().toInt() % 48 + 1))
//                    .apply {
//                        priceCent = 12345
//                        name = "${System.currentTimeMillis()}"
//                        barcode = "11023"
//                    }).apply { ELog.d(TAGS.machine, "update count:$this") }
//        }
    }

    @SuppressLint("SetTextI18n")
    private fun initBar() {
        // back
        binding.layoutTitle.back.visibility = View.GONE
        binding.layoutTitle.uuid.text = "机器编号：${machineInfo?.uuid}"
        binding.layoutTitle.title.setText(R.string.label_home_label)
    }

    private fun showDialog() {
        builder = AlertDialog.Builder(requireContext()).apply {
            setTitle("警告")
            setMessage("您点击了重启，点击确认后机器将会重新启动！")
            setPositiveButton("确定") { _, _ ->
                shell("reboot", StringBuilder())
            }
            setNegativeButton("取消") { d, _ ->
                d.dismiss()
            }.create().show()
        }
    }


}